package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.VehicleBrandDto;

public class VehicleBrandDaoImp extends TmsDao implements VehicleBrandDao {

	protected VehicleBrandDaoImp() throws TmsDaoException {
		super();
	}

	@Override
	public List<VehicleBrandDto> findAllVehicleBrandInfo() throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		VehicleBrandDto dto = null;
		List<VehicleBrandDto> list = new ArrayList<VehicleBrandDto>();
		
		try {
			conn = super.getConnection();
			
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT ");
			sql.append("ID        	,");
			sql.append("BRANDNAME,  ");
			sql.append("FIRSTCHAR,  ");
			sql.append("PINYIN, ");
			sql.append("VENDORID ");
			sql.append("FROM ");
			sql.append(TmsTableNames.TMS_VEHICLE_BRAND);
			sql.append(" order by  firstChar ");
			ps = conn.prepareStatement(sql.toString());
			
			// 执行sql
			rs = ps.executeQuery();

			while (rs.next()) {
				dto = new VehicleBrandDto();
				dto.setBrandId(rs.getString("ID"));
				dto.setBrandName(rs.getString("BRANDNAME"));
				dto.setBrandPinYin(rs.getString("PINYIN"));
				dto.setVendorId(rs.getString("VENDORID"));
				dto.setFirstChar(rs.getString("FIRSTCHAR"));
				list.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
			
		} finally {
			close(rs,ps);
		}
		return list;
	}

	@Override
	public String findVendorIdByBrandId(String brandId) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		String vendorId = "";
		try {
			conn = super.getConnection();
			
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT ");			
			sql.append("VENDORID ");
			sql.append("FROM ");
			sql.append(TmsTableNames.TMS_VEHICLE_BRAND);
			sql.append(" WHERE ");
			sql.append("ID=? ");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, brandId);
			// 执行sql
			rs = ps.executeQuery();

			while (rs.next()) {
				vendorId = rs.getString("VENDORID");
				
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
			
		} finally {
			close(rs,ps);
		}
		return vendorId;
	}
	
	@Override
	public String findBrandNameById(String id) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		String vendorId = "";
		try {
			conn = super.getConnection();
			
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT ");			
			sql.append("brandName ");
			sql.append("FROM ");
			sql.append(TmsTableNames.TMS_VEHICLE_BRAND);
			sql.append(" WHERE ");
			sql.append("ID=? ");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, id);
			// 执行sql
			rs = ps.executeQuery();

			while (rs.next()) {
				vendorId = rs.getString("brandName");
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
			
		} finally {
			close(rs,ps);
		}
		return vendorId;
	}
	
}

